﻿using System;
using System.Collections.Generic;
using System.Xml;
using SSP.Api.Interfaces;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DirectorGallery : DirectorWrapper, IDeserializer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorGallery"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public DirectorGallery(Director parent) : base(parent)
        {
        }

        /// <summary>
        /// The date and time the gallery was created.
        /// </summary>
        /// <value>The created.</value>
        public DateTime Created { get; set; }

        /// <summary>
        /// The date and time the gallery was last updated.
        /// </summary>
        /// <value>The modified.</value>
        public DateTime Modified { get; set; }


        /// <summary>
        /// The gallery description.
        /// </summary>
        /// <value>The description.</value>
        public string Description { get; set; }

        /// <summary>
        /// The gallery name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// The gallery's unique ID in the database.
        /// </summary>
        /// <value>The id.</value>
        public int Id { get; set; }

        /// <summary>
        /// The gallery's member albums and their content.
        /// </summary>
        /// <value>The albums.</value>
        public DirectorAlbum[] Albums { get; set; }

        #region IDeserializer Members

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> with the specified property name.
        /// </summary>
        /// <value></value>
        public string this[string propertyName]
        {
            get { return _sDict[propertyName]; }
            set { _sDict[propertyName] = value; }
        }

        private readonly Dictionary<string, string> _sDict = new Dictionary<string, string>();

        /// <summary>
        /// Deserializes from an XmlReader and sets the object's properties.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public void DeserializeFromReader(XmlReader reader)
        {
            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "id":
                        Id = reader.ReadElementContentAsInt();
                        break;
                    case "name":
                        Name = reader.ReadElementContentAsString();
                        break;
                    case "description":
                        Description = reader.ReadElementContentAsString();
                        break;
                    case "modified":
                        Modified = DirectorUtils.ConvertFromUnixTimestamp(reader.ReadElementContentAsString());
                        break;
                    case "created":
                        Created = DirectorUtils.ConvertFromUnixTimestamp(reader.ReadElementContentAsString());
                        break;
                    case "albums":
                        Albums = getAlbums(reader);
                        break;
                    default:
                        System.Diagnostics.Trace.WriteLine("Unhandled xml element name '" + reader.Name + "'.");
                        _sDict[reader.Name] = reader.ReadElementContentAsString();
                        break;
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();
        }

        #endregion

        /// <summary>
        /// Returns a single gallery along with its member albums and content.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public DirectorGallery Get(int id)
        {
            Parent.Post.Add("data[gallery_id]=" + id);
            DirectorResponse response = Parent.send("get_gallery", "get_gallery_" + id);
            return response.Gallery;
        }

        /// <summary>
        /// Return a list of all galleries along with their member albums. The content of the member albums is not returned. If you need to content, you should perform Album.Get while looping through the galleries albums.
        /// </summary>
        /// <returns></returns>
        public DirectorGallery[] All()
        {
            DirectorResponse response = Parent.send("get_gallery_list", "get_gallery_list");
            return response.Galleries;
        }
    }
}